using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.MessageHandlers;
using System;
using System.IO;
using System.Xml.Linq;
namespace Senparc.Weixin.MP.Agent
{
	public static class MessageAgent
	{
		private const int AGENT_TIME_OUT = 2500;
		public static string RequestXml(this IMessageHandler messageHandler, string url, string token, Stream stream, bool useWeiWeiHiKey = false, int timeOut = 2500)
		{
			if (messageHandler != null)
			{
				messageHandler.UsedMessageAgent = true;
			}
			string text = DateTime.Now.Ticks.ToString();
			string text2 = "GodBlessYou";
			string signature = CheckSignature.GetSignature(text, text2, token);
			url += string.Format("{0}signature={1}&timestamp={2}&nonce={3}", new object[]
			{
				url.Contains("?") ? "&" : "?",
				signature,
				text,
				text2
			});
			return RequestUtility.HttpPost(url, null, stream, null, null, null, timeOut, false);
		}
		public static string RequestXml(this IMessageHandler messageHandler, string url, string token, string xml, int timeOut = 2500)
		{
			if (messageHandler != null)
			{
				messageHandler.UsedMessageAgent = true;
			}
			string result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (StreamWriter streamWriter = new StreamWriter(memoryStream))
				{
					streamWriter.Write(xml);
					streamWriter.Flush();
					streamWriter.BaseStream.Position = 0L;
					result = messageHandler.RequestXml(url, token, streamWriter.BaseStream, false, timeOut);
				}
			}
			return result;
		}
		public static string RequestWeiWeiHiXml(this IMessageHandler messageHandler, string weiweihiKey, string xml, string weiweihiDomainName = "www.weiweihi.com", int timeOut = 2500)
		{
			if (messageHandler != null)
			{
				messageHandler.UsedMessageAgent = true;
			}
			string url = "http://" + weiweihiDomainName + "/App/Weixin?weiweihiKey=" + weiweihiKey;
			string result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (StreamWriter streamWriter = new StreamWriter(memoryStream))
				{
					streamWriter.Write(xml);
					streamWriter.Flush();
					streamWriter.BaseStream.Position = 0L;
					result = messageHandler.RequestXml(url, weiweihiKey, streamWriter.BaseStream, false, timeOut);
				}
			}
			return result;
		}
		public static IResponseMessageBase RequestResponseMessage(this IMessageHandler messageHandler, string url, string token, Stream stream, int timeOut = 2500)
		{
			return messageHandler.RequestXml(url, token, stream, false, timeOut).CreateResponseMessage();
		}
		public static IResponseMessageBase RequestResponseMessage(this IMessageHandler messageHandler, string url, string token, string xml, int timeOut = 2500)
		{
			return messageHandler.RequestXml(url, token, xml, timeOut).CreateResponseMessage();
		}
		public static IResponseMessageBase RequestWeiweihiResponseMessage(this IMessageHandler messageHandler, string weiweihiKey, string xml, string weiweihiDomainName = "www.weiweihi.com", int timeOut = 2500)
		{
			return messageHandler.RequestWeiWeiHiXml(weiweihiKey, xml, weiweihiDomainName, timeOut).CreateResponseMessage();
		}
		public static IResponseMessageBase RequestWeiweihiResponseMessage(this IMessageHandler messageHandler, string weiweihiKey, XDocument document, string weiweihiDomainName = "www.weiweihi.com", int timeOut = 2500)
		{
			return messageHandler.RequestWeiWeiHiXml(weiweihiKey, document.ToString(), weiweihiDomainName, timeOut).CreateResponseMessage();
		}
		public static IResponseMessageBase RequestWeiweihiResponseMessage(this IMessageHandler messageHandler, string weiweihiKey, RequestMessageBase requestMessage, string weiweihiDomainName = "www.weiweihi.com", int timeOut = 2500)
		{
			return messageHandler.RequestWeiWeiHiXml(weiweihiKey, requestMessage.ConvertEntityToXmlString<RequestMessageBase>(), weiweihiDomainName, timeOut).CreateResponseMessage();
		}
		public static bool CheckUrlAndToken(string url, string token, int timeOut = 2000)
		{
			bool result;
			try
			{
				string text = DateTime.Now.Ticks.ToString();
				string text2 = "GodBlessYou";
				string text3 = Guid.NewGuid().ToString("n");
				string signature = CheckSignature.GetSignature(text, text2, token);
				url += string.Format("{0}signature={1}&timestamp={2}&nonce={3}&echostr={4}", new object[]
				{
					url.Contains("?") ? "&" : "?",
					signature,
					text,
					text2,
					text3
				});
				string b = RequestUtility.HttpGet(url, null, null, timeOut);
				result = (text3 == b);
			}
			catch
			{
				result = false;
			}
			return result;
		}
	}
}
